package ${domain}.module.wsys.mgr;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ${domain}.frame.auth.Token;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.base.SortType;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.wsys.ent.DataCount;
import ${domain}.module.wsys.ent.DataTotal;
import ${domain}.module.wsys.ent.Logtask;
import ${domain}.module.wsys.mpr.DataMapper;
import ${domain}.module.wsys.req.DataErrorRequest;
import ${domain}.module.wsys.req.DataLoginRequest;
import ${domain}.module.wsys.req.DataTaskRequest;
import ${domain}.module.wsys.req.DataTotalRequest;
import ${domain}.module.wsys.req.LogtaskFindRequest;
import ${domain}.module.wsys.rsp.DataErrorResponse;
import ${domain}.module.wsys.rsp.DataLoginResponse;
import ${domain}.module.wsys.rsp.DataTaskResponse;
import ${domain}.module.wsys.rsp.DataTotalResponse;
import ${domain}.module.wsys.rsp.LogtaskFindResponse;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据业务类
 *
 * @author author
 * @version 0.0.1
 * @since 2017-01-01
 */
@Transactional
@Service
public class DataManagerImpl implements DataManager {

    @Autowired
    private DataMapper dataMapper;

    @Autowired
    private LogtaskManager logtaskManager;

    /**
     * 各业务数据统计
     *
     * @param request 请求对象
     * @param token   令牌
     * @return
     */
    @Override
    public DataTotalResponse total(DataTotalRequest request, Token token) {
        DataTotalResponse response = new DataTotalResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        DataTotal dataTotal = dataMapper.total(request, token);
        response.setDataTotal(dataTotal);
        return response;
    }

    /**
     * 登录统计
     *
     * @param request 请求对象
     * @param token   令牌
     * @return
     */
    @Override
    public DataLoginResponse login(DataLoginRequest request, Token token) {
        DataLoginResponse response = new DataLoginResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }
        SimpleDateFormat simpleDateFormat1 = null;
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            simpleDateFormat1 = new SimpleDateFormat(request.getFormat());
        } catch (Exception e) {
            response.addError(ErrorType.BUSINESS_ERROR, "日期格式化表达式错误");
            return response;
        }

        List<Map<String, String>> list = new ArrayList<>();
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        if (request.isToday()) {
            Map<String, String> datas = new HashMap<>();
            datas.put("date1", simpleDateFormat1.format(instance.getTime()));
            datas.put("date2", simpleDateFormat2.format(instance.getTime()));
            list.add(datas);
        }
        while (list.size() < request.getDays()) {
            instance.add(Calendar.DAY_OF_MONTH, -1);
            Map<String, String> datas = new HashMap<>();
            datas.put("date1", simpleDateFormat1.format(instance.getTime()));
            datas.put("date2", simpleDateFormat2.format(instance.getTime()));
            list.add(datas);
        }

        List<DataCount> result = dataMapper.login(list, token);
        response.setResult(result);
        return response;
    }

    /**
     * 故障统计
     *
     * @param request 请求对象
     * @param token   令牌
     * @return
     */
    @Override
    public DataErrorResponse error(DataErrorRequest request, Token token) {
        DataErrorResponse response = new DataErrorResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }
        SimpleDateFormat simpleDateFormat1 = null;
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            simpleDateFormat1 = new SimpleDateFormat(request.getFormat());
        } catch (Exception e) {
            response.addError(ErrorType.BUSINESS_ERROR, "日期格式化表达式错误");
            return response;
        }

        List<Map<String, String>> list = new ArrayList<>();
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        if (request.isToday()) {
            Map<String, String> datas = new HashMap<>();
            datas.put("date1", simpleDateFormat1.format(instance.getTime()));
            datas.put("date2", simpleDateFormat2.format(instance.getTime()));
            list.add(datas);
        }
        while (list.size() < request.getDays()) {
            instance.add(Calendar.DAY_OF_MONTH, -1);
            Map<String, String> datas = new HashMap<>();
            datas.put("date1", simpleDateFormat1.format(instance.getTime()));
            datas.put("date2", simpleDateFormat2.format(instance.getTime()));
            list.add(datas);
        }

        List<DataCount> result = dataMapper.error(list, token);
        response.setResult(result);
        return response;
    }

    /**
     * 任务统计
     *
     * @param request 请求对象
     * @param token   令牌
     * @return
     */
    @Override
    public DataTaskResponse task(DataTaskRequest request, Token token) {
        DataTaskResponse response = new DataTaskResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        LogtaskFindRequest logtaskFindRequest = new LogtaskFindRequest();
        logtaskFindRequest.setSortType(SortType.ASC);
        logtaskFindRequest.setSortKey("CREATE_TIME");
        logtaskFindRequest.setPageSize(100);
        LogtaskFindResponse logtaskFindResponse = logtaskManager.find(logtaskFindRequest, token);
        if (logtaskFindResponse.hasError()) {
            response.addErrors(logtaskFindResponse.getErrors());
            return response;
        }
        List<DataCount> result = new ArrayList<>();
        response.setResult(result);
        int countSuccess = 0;
        for (Logtask logtask : logtaskFindResponse.getResult()) {
            DataCount dataCount = new DataCount();
            dataCount.setDate("");
            dataCount.setCount(logtask.getExecTime());
            if ("1".equals(logtask.getExecState())) {
                countSuccess++;
            }
            result.add(dataCount);
        }
        response.setCountSuceess(countSuccess);
        response.setCountFailed(logtaskFindResponse.getResult().size() - countSuccess);
        return response;
    }
}
